#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h> 
#include <signal.h>    

long double c=0;
void catch(int sig_num)
{
	     signal(sig_num, catch);
	  
	     printf("\n==================\nSIGNAL: I Caught signal %d \n====================\n",sig_num);
	     printf("Total memory allocated till now:%LG \n",c/(1024.0*1024)); 	   
	     fflush(stdout);
	     exit(0);
}
void showhelp()
{
	printf("ZeroFillRam: keeps allocating memory in the RAM till the last possible word size. It can be used for the purpose of testing upper limits of memory resource or to test kernel's memory allocation capabilies.\n");
        printf("Author: Abhishek Parolkar (abhishek@parolkar.com)\n");
	printf("Syntax: ZeroFillRam <processor_word_size>\n");
        printf("  <processor_word_size>     Description          \n");
	printf("          16                for 16-bit processor \n");
	printf("          32                for 32-bit processor \n");
	printf("          64                for 64-bit processor \n");
	printf("Your processor generates following information: \n");
	system("cat /proc/cpuinfo ");
        exit(0);
}
int main(int argc,char *argv[])
{
	int word=4; // 32-bit Processor
        if(argc == 2)
	{
           if(strcmp(argv[1],"16")==0)
		   word = 2;
	   else if(strcmp(argv[1],"32")==0)
		   word = 4;
	   else if(strcmp(argv[1],"64")==0)
		   word = 8;
	   else showhelp();

	}
        else
	{
		showhelp();
                exit(0);
	}	
        char *p;
       	p= malloc(word);
	signal(SIGINT,catch);
        signal(SIGKILL,catch);
        signal(SIGQUIT,catch);
        signal(SIGABRT,catch);
        signal(SIGHUP,catch);
	signal(SIGSEGV,catch);
	signal(SIGTERM,catch);
	signal(SIGSTOP,catch);
	
	while(p)
        {
   	p= malloc(word);	
	if(p)
	{
         c=c+word;
	 *p=0;
 	 printf(" Allocated: %LG MB\n ", c/(1024.0*1024));
         }      	
	}
        
 printf("Total memory allocated sucessfully: %LG MB\n",c/(1024.0*1024));	
}

